/**
 * 
 */
package com.newworld.dmsapp2.activity;

/**
 * @author Administrator
 *
 */
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.BaseActivity;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.HttpUtil;
import com.newworld.dmsapp2.util.SingleSample;
import com.newworld.dmsapp2.util.Tool;

@SuppressLint("NewApi")
public class HouseManageMakeInvetecodeActivity extends BaseActivity implements
		OnClickListener {
	private Button btn_copy, btn_send;
	private TextView tv_code;
	private Intent intent;
	private String houseId;
	private String relationTypeId;
	private ProgressBar progressBar;
	private User user;
	private String inviteCode;
	private String message;
	private LinearLayout leftLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_house_manage_make_invetecode);
		initViews();
		initEvents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void initViews() {
		setTitlebarLeft(this, Config.TITLE_MAKE_INVETECODE,
				HouseManagePeopleListActivity.class);
		tv_code = (TextView) findViewById(R.id.house_manage_invetecode_tx);
		btn_copy = (Button) findViewById(R.id.hosue_manage_copy_code);
		btn_send = (Button) findViewById(R.id.hosue_manage_send_code);
		progressBar = (ProgressBar) findViewById(R.id.proBar_get_data);
		leftLayout = (LinearLayout) findViewById(R.id.titlebar_left_left_layout);

		user = SingleSample.getInstance(this).getUser();
		intent = getIntent();
		if (intent != null) {
			houseId = intent.getStringExtra("houseId");
			relationTypeId = intent.getStringExtra("relationTypeId");
		}
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected void onPreExecute() {
				if (Tool.isNetworkAvailable(HouseManageMakeInvetecodeActivity.this)) {
					progressBar.setVisibility(android.view.View.VISIBLE);
				} else {
					Toast.makeText(HouseManageMakeInvetecodeActivity.this,
							"请检查网络连接！", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			protected Void doInBackground(Void... params) {
				if (Tool.isNetworkAvailable(HouseManageMakeInvetecodeActivity.this)) {
					try {
						String url = Config.SERVER_URL
								+ user.getFunctionlistFromUser(
										Config.FUNCTION_REAL_GENERATERELATEHOUSEINVITECODE)
										.get(0).getFunctionUrl();

						Map<String, String> map = new HashMap<String, String>();
						map.put("functionCode",
								Config.FUNCTION_REAL_GENERATERELATEHOUSEINVITECODE);
						map.put("houseId", houseId);
						map.put("relationTypeId", relationTypeId);
						Log.e("获取邀请码relationTypeId", relationTypeId);
						String setsessionid = "";
						user = SingleSample.getInstance(HouseManageMakeInvetecodeActivity.this).getUser();
						if(null != user){
							if(null != user.getSessionId() && !user.getSessionId().equals("")){
								setsessionid = user.getSessionId();
							}
						}
						String result = HttpUtil.postRequestSe(url, map,setsessionid);
						Log.e("获取邀请码result", result);

						if (new JSONObject(result).getString("flag").equals(
								"success")) {
							String rows = new JSONObject(result)
									.getString("obj");

							inviteCode = new JSONObject(rows)
									.getString("inviteCode");
							message = new JSONObject(rows)
									.getString("inviteMessage");

						} else {
							message = new JSONObject(result)
									.getString("message");

						}

					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				return null;
			}

			@Override
			protected void onPostExecute(Void result) {

				progressBar.setVisibility(View.INVISIBLE);
				Toast.makeText(HouseManageMakeInvetecodeActivity.this, message,
						Toast.LENGTH_SHORT).show();
				tv_code.setText(inviteCode);

			}
		}.execute(null, null, null);
	}

	@Override
	protected void initEvents() {
		btn_copy.setOnClickListener(this);
		btn_send.setOnClickListener(this);
		leftLayout.setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 复制按钮
		case R.id.hosue_manage_copy_code:
			ClipboardManager cmb = (ClipboardManager) this
					.getSystemService(Context.CLIPBOARD_SERVICE);
			cmb.setText(tv_code.getText().toString());
			showShortToast("复制成功！");
			break;
		// 发送短信
		case R.id.hosue_manage_send_code:
			Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
			sendIntent.setData(Uri.parse("smsto:"));
			sendIntent.putExtra("sms_body", tv_code.getText().toString());
			startActivity(sendIntent);
			break;
		// 返回
		case R.id.titlebar_left_left_layout:
			Intent intent = new Intent(HouseManageMakeInvetecodeActivity.this,
					HouseManagePeopleListActivity.class);
			intent.putExtra("houseId", houseId);
			startActivity(intent);
			finish();
		default:

			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键
			Intent intent = new Intent(HouseManageMakeInvetecodeActivity.this,
					HouseManagePeopleListActivity.class);
			intent.putExtra("houseId", houseId);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
